﻿using DDU.Infrastructure;
using System;
using System.Collections.Generic;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;

namespace DDU.DataModel
{
    [Table(Name = "T_Activities")]
    public class ActivityInfo : NotificationObject
    {
        #region Fields
        private string _guid = System.Guid.NewGuid().ToString();
        private string _title;
        private string _content;
        private string _location;
        private bool _afterClass = true;
        private bool _allDayActivity = true;
        private string _remarks;
        private DateTime _startTime=DateTime.Now;
        private DateTime _endTime = DateTime.Now;
        #endregion

        #region Properties
        [Column(IsPrimaryKey = true)]
        public string Guid
        {
            get { return _guid; }
            set
            {
                _guid = value;
                RaisePropertyChanged(() => Guid);
            }
        }

        [Column]
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged(() => Title);
            }
        }

        [Column]
        public string Content
        {
            get { return _content; }
            set
            {
                _content = value;
                RaisePropertyChanged(() => Content);
            }
        }

        [Column]
        public string Location
        {
            get { return _location; }
            set
            {
                _location = value;
                RaisePropertyChanged(() => Location);
            }
        }

        [Column]
        public bool AfterClass
        {
            get { return _afterClass; }
            set
            {
                _afterClass = value;
                RaisePropertyChanged(() => AfterClass);
            }
        }

        [Column]
        public bool AllDayActivity
        {
            get { return _allDayActivity; }
            set
            {
                _allDayActivity = value;
                RaisePropertyChanged(() => AllDayActivity);
            }
        }

        [Column]
        public string Remarks
        {
            get { return _remarks; }
            set
            {
                _remarks = value;
                RaisePropertyChanged(() => Remarks);
            }
        }

        [Column]
        public DateTime StartTime
        {
            get { return _startTime; }
            set
            {
                _startTime = value;
                RaisePropertyChanged(() => StartTime);
                RaisePropertyChanged(() => Duration);
            }
        }

        [Column]
        public DateTime EndTime
        {
            get { return _endTime; }
            set
            {
                _endTime = value;
                RaisePropertyChanged(() => EndTime);
                RaisePropertyChanged(() => Duration);
            }
        }

        public double Duration
        {
            get
            {
                
                TimeSpan tsStart = TimeSpan.FromTicks(StartTime.Ticks);
                TimeSpan tsEnd = TimeSpan.FromTicks(EndTime.Ticks);

                TimeSpan ts = tsStart.Subtract(tsEnd).Duration();

                if (ts.TotalHours < 0.00)
                {
                    return 0;
                }
                if (EndTime < StartTime)
                {
                    return 0 - ts.TotalHours;
                }
                return ts.TotalHours;
            }
        }
        #endregion

        #region Event
        public event EventHandler<PropertyArgs> ValueChanged = delegate { };

        protected override void RaisePropertyChanged(string propertyName)
        {
            base.RaisePropertyChanged(propertyName);
            ValueChanged.Invoke(this, new PropertyArgs()
            {
                Property = GetType().GetProperty(propertyName)
            });
        }
        #endregion
    }
}
